import '../../tools_base/debug_log.dart';

///用户信息
class UserInfoModel {
  int? age;
  int? like;
  int? follows;
  int? fans;
  int? collectionCount;
  int? likeCount;
  bool? isVip;
  bool? isFollow;
  String? promoteURL;
  String? inviterCode;
  int? uid;
  String? devID;
  String? devToken;
  String? mobile;
  String? gender;
  String? name;
  String? promotionCode;
  String? portrait;
  String? summary;
  int? vipLevel; // 1：影片会员，2：ACG会员，3:超级会员 （一卡通）
  String? vipExpireDate;
  String? createdAt;
  int? urrPortraitStatus; //头像审核状态
  bool? hasLocked;
  bool? hasBanned;
  String? token;
  bool? officialCert; //官方认证
  String? upTag;

  String? vipName;
  bool allGoldVideoFree = false;
  List<int>? awards;
  int? level; //当前等级
  String? rewarded;
  int? loginType; //登录类型修改  0-devID 1-token 2 mobileLogin 3-qrCodeLogin
  String? goldVideoFreeExpire;
  List<MyCoupon>? goldVideoCoupon;
  String? videoFreeExpiration;
  int? worksTotal;
  int? downloadCount;
  int? hasShop;
  bool? collectionStatus; //收藏 ，fasle 隐藏
  bool? movieStatus; // 作品，fasle 隐藏
  bool? postStatus; // 动态，fasle 隐藏
  int? goldVideoFreeLimit;
  String? broadcastExpire; //用户观看直播权益的有效期
  int? vidUploadCount;
  int? watchCount;
  int? coverUploadCount;
  int get totalWorkCount{
    return (vidUploadCount ?? 0) + (coverUploadCount ?? 0);
  }
  bool isVipExpired() {
    // true: vip过期了
    try {
      var startDate = goldVideoFreeExpire;
      var endDate = DateTime.now();
      return DateTime.parse(startDate ?? "").isBefore(endDate);
    } catch (e) {
      debugLog(e);
      return true;
    }
  }

  bool get isRechargeVIP {
    return (isVip ?? false) && (vipLevel ?? 0) > 0;
  }

  // 是否为店主
  bool get isStoreOwner {
    if (hasShop == null || hasShop == 0 || hasShop == 1 || hasShop == 3) return false;
    return true;
  }

  //vip到期时间img
  String get vipDateImg {
    if (isVip == true) {
      if ((vipLevel ?? 0) < 3) {
        return 'mine_vip_badgesuper.webp';
      } else {
        return 'mine_vip_badge.webp';
      }
    } else {
      return '';
    }
  }

  String get genderIcon {
    if (gender == "man") {
      return "male";
    } else if (gender == "woman") {
      return "female";
    }
    return gender ?? "";
  }

  static UserInfoModel fromMap(Map<String, dynamic>? map) {
    map ??= {};

    UserInfoModel info = UserInfoModel();
    if (map.containsKey("allGoldVideoFree")) {
      info.allGoldVideoFree = map['allGoldVideoFree'];
    } else {
      info.allGoldVideoFree = false;
    }
    info.vidUploadCount = map['vidUploadCount'];
    info.watchCount = map['watchCount'];
    info.coverUploadCount = map['coverUploadCount'];
    info.upTag = map['upTag'];
    info.goldVideoFreeLimit = map['goldVideoFreeLimit'];
    info.vipName = map['vipName'];
    info.collectionStatus = map['collectionStatus'];
    info.allGoldVideoFree = map['allGoldVideoFree'];
    info.movieStatus = map['movieStatus'];
    info.postStatus = map['postStatus'];
    info.hasShop = map['hasShop'];
    info.downloadCount = map["downloadCount"];
    info.age = map['age'];
    info.like = map['like'];
    info.follows = map['follows'];
    info.fans = map['fans'];
    info.awards = (map['awards'] as List?)?.map((e) => int.tryParse(e.toString()) ?? 0).toList();
    info.collectionCount = map['collectionCount'];
    info.likeCount = map['likeCount'];
    info.isVip = map['isVip'];
    info.isFollow = map['isFollow'];
    info.promoteURL = map['promoteURL'];
    info.inviterCode = map['inviterCode'];
    info.uid = map['uid'];
    info.devID = map['devID'];
    info.devToken = map['devToken'];
    info.mobile = map['mobile'];
    info.gender = map['gender'];
    info.name = map['name'];
    info.promotionCode = map['promotionCode'];
    info.portrait = map['portrait'];
    info.summary = map['summary'];
    info.vipLevel = map['vipLevel'];
    info.vipExpireDate = map['vipExpireDate'];
    info.createdAt = map['createdAt'];
    info.hasLocked = map['hasLocked'];
    info.hasBanned = map['hasBanned'];
    info.token = map['token'];
    info.officialCert = map['officialCert'];
    info.rewarded = map['rewarded'];
    info.loginType = map['loginType'];
    info.worksTotal = map['worksTotal'];

    info.goldVideoCoupon = (map['goldVideoCoupon'] as List?)?.map((o) {
      return MyCoupon.fromJson(o);
    }).toList();
    info.level = map['level'];
    info.goldVideoFreeExpire = map['goldVideoFreeExpire'];
    info.videoFreeExpiration = map['videoFreeExpiration'];
    info.broadcastExpire = map['broadcastExpire'];
    info.urrPortraitStatus = map['urrPortraitStatus'];

    return info;
  }

  static UserInfoModel fromJson(Map<String, dynamic>? map) => fromMap(map);

  Map toJson() => {
        "allGoldVideoFree": allGoldVideoFree,
        "age": age,
        "like": like,
        "follows": follows,
        "fans": fans,
        "collectionCount": collectionCount,
        "likeCount": likeCount,
        "isVip": isVip,
        "worksTotal": worksTotal,
        "isFollow": isFollow,
        "promoteURL": promoteURL,
        "inviterCode": inviterCode,
        "uid": uid,
        "devID": devID,
        "devToken": devToken,
        "mobile": mobile,
        "gender": gender,
        "name": name,
        "promotionCode": promotionCode,
        "portrait": portrait,
        "summary": summary,
        "vipLevel": vipLevel,
        "vipExpireDate": vipExpireDate,
        "createdAt": createdAt,
        "hasLocked": hasLocked,
        "hasBanned": hasBanned,
        "token": token,
        "rewarded": rewarded,
        "officialCert": officialCert,
        "goldVideoCoupon": goldVideoCoupon,
        "level": level,
        "loginType": loginType,
        "goldVideoFreeExpire": goldVideoFreeExpire,
        "uservideoFreeExpiration": videoFreeExpiration,
        "isStoreOwner": isStoreOwner,
        "urrPortraitStatus": urrPortraitStatus,
      };
}

class MyCoupon {
  int? gold;
  int? count;

  static MyCoupon fromMap(Map<String, dynamic>? map) {
    map ??= {};

    MyCoupon myCoupon = MyCoupon();
    myCoupon.gold = map['gold'];
    myCoupon.count = map['count'];

    return myCoupon;
  }

  static MyCoupon fromJson(Map<String, dynamic>? map) => fromMap(map);
}

class DetailInfo {
  String id;
  bool hasReturn;
  int totalDays;
  int currentSignDays;
  int forgetSignDays;
  int renewalSignDays;
  int renewalSignPrice;
  String startTime;
  String endTime;
  List<RecordTimeModel>? recordTime;

  DetailInfo({
    this.id = "",
    this.hasReturn = false,
    this.totalDays = 0,
    this.currentSignDays = 0,
    this.forgetSignDays = 0,
    this.renewalSignDays = 0,
    this.renewalSignPrice = 0,
    this.startTime = "",
    this.endTime = "",
    this.recordTime,
  });

  factory DetailInfo.fromJson(Map<String, dynamic> json) => DetailInfo(
        id: json["id"],
        hasReturn: json["hasReturn"],
        totalDays: json["totalDays"],
        currentSignDays: json["currentSignDays"],
        forgetSignDays: json["forgetSignDays"],
        renewalSignDays: json["renewalSignDays"],
        renewalSignPrice: json["renewalSignPrice"],
        startTime: json["startTime"],
        endTime: json["endTime"],
        recordTime:
            json["recordTime"] != null ? List<RecordTimeModel>.from(json["recordTime"].map((x) => RecordTimeModel.fromJson(x))) : [],
      );

  Map<String, dynamic> toJson() => {
        "id": id,
        "hasReturn": hasReturn,
        "totalDays": totalDays,
        "currentSignDays": currentSignDays,
        "forgetSignDays": forgetSignDays,
        "renewalSignDays": renewalSignDays,
        "renewalSignPrice": renewalSignPrice,
        "startTime": startTime,
        "endTime": endTime,
        "recordTime": recordTime == null ? [] : List<RecordTimeModel>.from(recordTime!.map((x) => x.toJson())),
      };

  @override
  String toString() {
    return 'DetailInfo{id: $id, hasReturn: $hasReturn, totalDays: $totalDays, currentSignDays: $currentSignDays, forgetSignDays: $forgetSignDays, renewalSignDays: $renewalSignDays, renewalSignPrice: $renewalSignPrice, startTime: $startTime, endTime: $endTime, recordTime: $recordTime}';
  }
}

class RecordTimeModel {
  String signTime = "";
  bool isSign = false;

  RecordTimeModel({this.signTime = "", this.isSign = false});

  @override
  String toString() {
    return 'RecordTimeModel{signTime: $signTime, isSign: $isSign}';
  }

  factory RecordTimeModel.fromJson(Map<String, dynamic> json) => RecordTimeModel(
        signTime: json["signTime"],
        isSign: json["isSign"],
      );

  Map<String, dynamic> toJson() => {
        "signTime": signTime,
        "isSign": isSign,
      };
}
